import type { DictPayload } from '@/store/modules/dict'
import { fetchDictCache } from '@/api/system/dict'

// 异步获取全部字典缓存（或传入类型数组获取部分）
export async function loadPreloadDicts(types?: string[]): Promise<DictPayload> {
  const data = await fetchDictCache(types)
  // 接口返回即为按类型分组的字典项列表，进行排序规范化
  const result: DictPayload = {}
  Object.keys(data || {}).forEach((key) => {
    const list = [...(data[key] || [])]
    result[key] = list.sort((a, b) => (a.sort_order || 0) - (b.sort_order || 0))
  })
  return result
}